package com.skit.mine.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.skit.common.bean.Department;
import com.skit.mine.R;

import java.util.List;

/**
 * @author shuike <a href="mailto:shuike007@gmail.com">Contact me.</a>
 * @version 1.0
 * @since 2020/5/23
 */
public class SingleTextAdapter extends RecyclerView.Adapter<SingleTextAdapter.ViewHolder> {
    private List<Department> list;
    private int selectPosition = 0;
    private int type;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(Department data, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public SingleTextAdapter(List<Department> list, int type) {
        this.list = list;
        this.type = type;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_single_text_item, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Department department = (Department) list.get(position);
        holder.content.setText(department.getDepartmentName());
//        if (selectPosition == position) {
//            holder.content.setBackgroundColor(ResourcesManager.getRes().getColor(R.color.itemBg));
//        } else {
//            holder.content.setBackgroundColor(Color.TRANSPARENT);
//        }
        holder.itemView.setOnClickListener(v -> {
            selectPosition = position;
            notifyDataSetChanged();
            if (onItemClickListener != null) onItemClickListener.onItemClick(department, position);
        });
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public int getSelectPosition() {
        return selectPosition;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView content;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.content);
        }
    }
}
